package services

import (
	"fmt"
	"gopkg.in/mail.v2"
	"io"
	"mygo/app/models"
	"os"
	"path/filepath"
	"strings"
)

type sendService struct {
}

var Send = sendService{}

// SendEmail 向邮箱中发送文件
func (s *sendService) SendEmail() {
	smtpHost := "smtp.qq.com"
	smtpPort := 465
	smtpUser := ""
	smtpPass := ""

	form := "792505700@qq.com"
	to := []string{"1544016288@qq.com"}

	subject := "Test Email"
	body := "This is a test email sent using Golang."

	m := mail.NewMessage()
	m.SetHeader("From", form)
	m.SetHeader("To", to...)
	m.SetHeader("Subject", subject)
	m.SetBody("text/html", body)

	d := mail.NewDialer(smtpHost, smtpPort, smtpUser, smtpPass)
	d.StartTLSPolicy = mail.MandatoryStartTLS

	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}

}

func GuiDang(fileName, baseUrl string, alarmDetail *models.AlarmDetail) error {
	timeNow := alarmDetail.Time
	year := strings.Split(timeNow, "-")[0]

	//file, err := os.Open(fileName)
	//if err != nil {
	//	return err
	//}
	//defer file.Close()

	//获取文件名\\Chee\0局域网共享文件夹测试
	//dirName := baseUrl + "09_警报单" + "\\" + year + "\\" + alarmDetail.Subject + "\\" + name1 + alarmDetail.Subject + "\\" + regionMap[alarmDetail.Region]
	dirName := ""
	if alarmDetail.Type != "天文潮" {
		name1 := strings.Split(strings.Split(alarmDetail.Bh, " ")[1], "-")[0]
		dirName = baseUrl + "警报单" + "\\" + year + "\\" + alarmDetail.Subject + "\\" + name1 + alarmDetail.Subject + "\\" + regionMap[alarmDetail.Region]
	} else {
		dirName = baseUrl + "警报单" + "\\" + year + "\\" + "天文大潮消息" + "\\" + regionMapTwc[alarmDetail.Region]
	}
	//判断文件夹是否存在，不存在则创建
	if _, err := os.Stat(dirName); err != nil {
		if os.IsNotExist(err) {
			err := os.MkdirAll(dirName, os.ModePerm)
			if err != nil {
				return err
			}
		}
	}
	//遍历文件夹，打印所有文件名
	fileList := []string{}
	filepath.Walk(dirName, func(path string, f os.FileInfo, err error) error {
		fileList = append(fileList, f.Name())
		return nil
	})
	//判断fileList的所有文件名中是否有包含alarmDetail.Bh的文件名，如果有，则删除
	for _, v := range fileList {
		if strings.Contains(v, alarmDetail.Bh) {
			err := os.Remove(dirName + "\\" + v)
			if err != nil {
				return err
			}
		}
	}

	types := [2]string{".docx", ".pdf"}
	//types := [1]string{".docx"}
	nameFirst := strings.Split(alarmDetail.FileName, ".")[0]

	for _, v := range types {
		fileNameFinal := dirName + "\\" + nameFirst + v
		//fileNameFinal := "C:\\Users\\chun\\GolandProjects\\sa-go\\public\\测试归档" + "\\" + nameFirst + v
		oldFileName := "./public/jb/" + strings.Split(alarmDetail.FileName, ".")[0] + v
		file, err := os.Open(oldFileName)
		//创建文件
		f, err := os.Create(fileNameFinal)
		if err != nil {
			//tools.ErrorResp(c, 500, "文件创建失败")
			return err
		}
		//将文件复制到f
		_, err = io.Copy(f, file)
		if err != nil {
			//tools.ErrorResp(c, 500, "文件复制失败")
			return err
		}
		f.Close()
		file.Close()
	}
	return nil
}

// SetWxModel string first, string k1, string k2, string k3, string remark
func SetWxModel(strArr [][2]string) map[string]map[string]string {
	m := make(map[string]map[string]string)
	for _, str := range strArr {
		m[str[0]] = map[string]string{
			"value": str[1],
			"color": "#000000",
		}
	}
	fmt.Println(m)
	return m
}
